package com.itheima02.controller;

import com.itheima02.domain.Student;
import com.itheima02.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/updateStudentServlet")
public class UpdateStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id") ;
        System.out.println(id);
        String name=req.getParameter("name");
        System.out.println(name);
        Integer age = Integer.parseInt(req.getParameter("age"));
        System.out.println(age);
        String birthday=req.getParameter("birthday");
        System.out.println(birthday);

        StudentService service=new StudentService();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date parse = sdf.parse(birthday);

            Student stu=new Student(Integer.parseInt(id),name,age,parse);
            service.update(stu);
            System.out.println(stu);
          resp.sendRedirect("/studentServlet");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
